package domain.update;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.data.QnAPersistenceManager;
import domain.model.Domain;
import domain.service.SelfSiteUpdate;

@SuppressWarnings("serial")
public class InfoUpdateServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.getWriter().println("Hello, world");
        
        PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
        String title_url = req.getParameter("name");
		title_url = title_url.toLowerCase().replaceAll("(http|https)+://(www.)*", "").replaceAll("^[\\w\\W]+[\\/]{2}", "").replaceAll("[\\/]+[\\w\\W]+$", "").replaceAll("www.", "");
        new SelfSiteUpdate(title_url);
        
        @SuppressWarnings("unchecked")
		List<Domain> check =  (List<Domain>)psm.newQuery(Domain.class,"name == '"+title_url+"'").execute();
	    
	    if(check.get(0).getDescription()!= null && check.get(0).getDescription().equals("") || check.get(0).getDescription() ==  null)
	    {
		    new SelfSiteUpdate("www."+title_url);
	    }
    }
}
